<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="供应商" prop="purchaseManufactor">
        <el-select v-model="queryParams.purchaseManufactor" placeholder="请选择供应商" clearable size="small">
          <el-option
            v-for="dict in manufactorlist"
            :key="dict.manufactorId"
            :label="dict.manufactorName"
            :value="dict.manufactorId"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="申请人" prop="purchaseApplicant">
          <el-input
            v-model="queryParams.purchaseApplicant"
            placeholder="请输入申请人"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.purchaseStatus" placeholder="入库状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['his:purchase:add']"
        >新增采购
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate"
          v-hasPermi="['his:purchase:edit']"
        >提交审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['his:purchase:remove']"
        >作废
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate"
          v-hasPermi="['his:purchase:edit']"
        >提交入库
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="单据ID" align="center">
        <template slot-scope="scope">
          <router-link :to="'purchase/pData/'+ scope.row.purchasePId" class="link-type">
            <span>{{ scope.row.purchasePId }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="purchaseManufactor" :formatter="drugsManufactorFormat"/>
      <el-table-column label="采购批发总额" align="center" prop="purchasePrice"/>
      <el-table-column label="状态" align="center" prop="purchaseStatus" :formatter="statusFormat"/>
      <el-table-column label="申请人" align="center" prop="purchaseApplicant"/>
      <el-table-column label="入库操作人" align="center" prop="purchaseOperator"/>
      <el-table-column label="入库时间" align="center" prop="updateTime"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase } from '@/api/his/purchase'
  import { drugsManufactorList } from '@/api/his/drugs'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
    name: 'Purchase',
    components: { Treeselect },
    data() {
      return {

        manufactorlist:'',
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 单据表格数据
        purchaseList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          purchaseManufactor: undefined,
          purchaseApplicant: undefined,
          purchaseStatus: undefined,
        },
        // 表单参数
        form: {},
      }
    },
    created() {
      this.getList()
      this.getDicts('dms_purchase_status').then(response => {
        this.statusOptions = response.data
      })
    },
    methods: {

      // 药品厂家格式化
      drugsManufactorFormat(row, column) {
        var actions = [];
        Object.keys(this.manufactorlist).some((key) => {
          if (this.manufactorlist[key].manufactorId == ('' + row.purchaseManufactor)) {
            actions.push(this.manufactorlist[key].manufactorName);
            return true;
          }
        })
        return actions.join('');
      },

      // 字典状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.purchaseStatus)
      },

      /** 查询单据列表 */
      getList() {
        this.loading = true
        drugsManufactorList().then(response =>{
          let j
          for (j = 0 ;j <response.length;j++){
            response[j].manufactorId = response[j].manufactorId.toString()
          }
          this.manufactorlist = response
        })
        listPurchase(this.queryParams).then(response => {
          this.purchaseList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          purchaseId: undefined,
          purchaseName: undefined,
          purchaseKeyword: undefined,
          purchasePrice: 0,
          purchaseCost: 0,
          purchaseUnit: undefined,
          purchaseCategory: undefined,
          status: '0'
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.purchaseId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加单据'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const purchaseId = row.purchaseId || this.ids
        getPurchase(purchaseId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改单据'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.purchaseId != undefined) {
              updatePurchase(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addPurchase(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const purchaseIds = row.purchaseId || this.ids
        this.$confirm('是否确认删除单据编号为"' + purchaseIds + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delPurchase(purchaseIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有单据数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.exportLoading = true
          return exportPurchase(queryParams)
        }).then(response => {
          this.download(response.msg)
          this.exportLoading = false
        }).catch(() => {
        })
      }
    }
  }
</script>
